﻿#include "FixedListView.h"

namespace LibraryEditor
{

    void FixedListView::OnKeyDown(KeyEventArgs *e)
    {
        _shiftOn = e->Shift;
        ListView::OnKeyDown(e);
    }

    void FixedListView::OnKeyUp(KeyEventArgs *e)
    {
        _shiftOn = false;
        ListView::OnKeyUp(e);
    }

    void FixedListView::OnMouseDown(MouseEventArgs *e)
    {
        ListViewItem *it = GetItemAt(e->X, e->Y);

        if (it == nullptr)
        {
            //lastItemIndexClicked2 = -1;
        }
        else
        {
            _lastItemIndexClicked2 = it->Index;
        }
        if (!_shiftOn || (_lastItemIndexClicked1 < 0))
        {
            _lastItemIndexClicked1 = _lastItemIndexClicked2;
        }
        ListView::OnMouseDown(e);
    }

    void FixedListView::OnVirtualItemsSelectionRangeChanged(ListViewVirtualItemsSelectionRangeChangedEventArgs *e)
    {
        try
        {
            int start = _lastItemIndexClicked1;
            int end = _lastItemIndexClicked2;
            if (end < start)
            {
                int temp = start;
                start = end;
                end = temp;
            }
            if ((start >= 0) && (end >= 0))
            {
                ArrayList *toRemove = new ArrayList();
                for (int index : SelectedIndices)
                {
                    if ((index < start) || (index > end))
                    {
                        toRemove->Add(index);
                    }
                }
                if (toRemove->Count > 0)
                {
                    for (auto index : *toRemove)
                    {
                        SelectedIndices->Remove(index);
                    }
                }

//C# TO C++ CONVERTER TODO TASK: A 'delete toRemove' statement was not added since toRemove was passed to a method or constructor. Handle memory management manually.
            }
            ListViewVirtualItemsSelectionRangeChangedEventArgs *te = new ListViewVirtualItemsSelectionRangeChangedEventArgs(start, end, e->IsSelected);
            ListView::OnVirtualItemsSelectionRangeChanged(te);

//C# TO C++ CONVERTER TODO TASK: A 'delete te' statement was not added since te was passed to a method or constructor. Handle memory management manually.
        }
        catch (...)
        {
        }
    }
}
